/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SaleManager.Order.view;

import Data.DAO.IDataStore;
import data.DAO.IOrderStore;
import data.pojo.Order;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author Kite
 */
public class OrderManage {
    
    private List<Order> searchList = new ArrayList<Order>();
    private List<Order> editList = new ArrayList<Order>();
    private ArrayList<Integer> indexes = new ArrayList<Integer>();
    private IDataStore orderDS = new IOrderStore();
    private List<Order> allOrders;
    
    private List<Order> testList = new ArrayList<Order>();
    
    private Order ot  = new Order();
    private Order ott  = new Order();
    
    public OrderManage(){
        //orderDS.update(testList);
        allOrders = (ArrayList<Order>)orderDS.getObjects();
    }
    
    public void test(){
        ot.setOrderID("1111");
        ot.setAddress("1");
        ot.setStatus("刚下单");
        ott.setOrderID("222");
        ott.setAddress("1");
        ott.setStatus("刚下单");
        testList.add(ot);
        testList.add(ott);
        orderDS.update(testList);
        testList = new ArrayList<Order>();
    }
    
       public Order getBySearch(int index) {
        return this.searchList.get(index);
    }
 

    public int getIndexInAll(Order c) {
        for(int i=0; i<allOrders.size(); i++){
            if(allOrders.get(i).getOrderID().equals(c.getOrderID())){
                return i;
            }
        }
        return -1;
    } 
    
       public boolean addToEditList(int index,Order c) {
      
            this.indexes.add(index);
            this.editList.add(c);
            return true;

    }


    
     public boolean commit() {
        for(int i=0; i<indexes.size(); i++){
            allOrders.set(this.indexes.get(i), this.editList.get(i));
        }
        orderDS.update(allOrders);
        this.editList.clear();
        this.indexes.clear();
        return true;
    }
    
    //刚下单、仓库配货、物流配送、已签收
    public boolean justTOpicking(Order o){
        if(!o.getStatus().equals("刚下单")){
            return false;
        }
        o.setStatus("仓库配货");
        return true;
    }
    
    public boolean pickingTOjust(Order o){
        if(!o.getStatus().equals("仓库配货")){
            return false;
        }
        o.setStatus("刚下单");
        return true;
    }
    
    public boolean pickingTOtransport(Order o){
        if(!o.getStatus().equals("仓库配货")){
            return false;
        }
        o.setStatus("物流配送");
        return true;
    }
    
    public boolean transportTOpicking(Order o){
        if(!o.getStatus().equals("物流配送")){
            return false;
        }
        o.setStatus("仓库配货");
        return true;
    }
    
    public boolean transportTOchecked(Order o){
        if(!o.getStatus().equals("物流配送")){
            return false;
        }
        o.setStatus("已签收");
        return true;
    }
    
    public boolean checkedTOtransport(Order o){
        if(!o.getStatus().equals("已签收")){
            return false;
        }
        o.setStatus("物流配送");
        return true;
    }
    
    public Vector<List> searchTableVector(String key, String keywords) {
         searchList = orderDS.getObjectsByAttr(key, ".*" + keywords + ".*");
        return new Vector<List>(toOrderTable(searchList));
    }

    public Vector<Vector<String>> toOrderTable(List list) {
        try {
            Vector<Order> seList = new Vector(list);
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for (int i = 0; i < seList.size(); i++) {
                reList.add((Vector<String>) toOrderStringArray(seList.get(i)));
            }
            return reList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List toOrderStringArray(Order order) {
        Vector<String> list = new Vector<String>();
        list.add(order.getOrderID());//Id
        list.add("");//day
        list.add(order.getCustomerID());//customer
        list.add(((Double) order.getDealAmount()).toString());//price     
        list.add(order.getRecipients());//Recipients
        list.add(order.getAddress());//Address
        list.add(order.getPostcode());//post
        list.add(order.getStatus());//状态
        return list;
    }
    
}
